package com.wondersgroup.wuhan_demo.card;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.wondersgroup.wuhan_demo.Constant;
import com.wondersgroup.wuhan_demo.R;
import com.wondersgroup.wuhan_demo.base.BaseActivity;
import com.wondersgroup.wuhan_demo.net.MyCallbackBean;
import com.wondersgroup.wuhan_demo.net.OkHttpHelper;
import com.wondersgroup.wuhan_demo.net.Param;
import com.zhy.http.okhttp.OkHttpUtils;

import org.dom4j.Element;

import java.util.Vector;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.MediaType;


public class ScanCardActivity extends BaseActivity {
    @BindView(R.id.tv_sfzhm)
    TextView tvSfzhm;
    @BindView(R.id.tv_xm)
    TextView tvXm;
    @BindView(R.id.tv_xb)
    TextView tvXb;
    private String TAG = this.getClass().getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_card);
        ButterKnife.bind(this);
        initTitleBar("个人信息");
        getAuthKey("441355122", "441355122", "123456a_");
    }

    private void getAuthKey(String orgCode, String userName, String passWord) {
        Vector v = new Vector<Param>();
        v.add(new Param("orgCode", orgCode));
        v.add(new Param("userName", userName));
        v.add(new Param("passWord", passWord));
        OkHttpUtils.postString()
                .url(Constant.URL)
                .tag(TAG)
                .headers(OkHttpHelper.getHeaders())
                .mediaType(MediaType.parse("text/xml;charset=UTF-8"))
                .content(OkHttpHelper.requestContent(v, "getAuthKey"))
                .build()
                .execute(new MyCallbackBean<Element>() {
                    @Override
                    public void onError(Call call, Exception e) {

                    }

                    @Override
                    public void onResponse(Element response) {
                        if (response != null) {
                            String message = response.element("message").getText();
                            String resultCode = response.element("resultCode").getText();
                            if ("10000".equals(resultCode)) {
                                String authKey = response.element("authKey").getText();
                                getInfo("441355122", authKey, "5", "EAC0DFE64166DC059FF92E58756C5E9849AEDB339ACCD11524BA08FE537E9AEF:1");
                            } else {
                                Toast.makeText(getApplicationContext(), message + "", Toast.LENGTH_SHORT).show();
                            }
                        }

                    }
                });
    }

    private void getInfo(String orgCode, String authKey, String type, String numStr) {
        Vector v = new Vector<Param>();
        v.add(new Param("orgCode", orgCode));
        v.add(new Param("authKey", authKey));
        v.add(new Param("type", type));
        v.add(new Param("numStr", numStr));
        OkHttpUtils.postString()
                .url(Constant.URL)
                .tag(TAG)
                .headers(OkHttpHelper.getHeaders())
                .mediaType(MediaType.parse("text/xml;charset=UTF-8"))
                .content(OkHttpHelper.requestContent(v, "getInfo"))
                .build()
                .execute(new MyCallbackBean<Element>() {
                    @Override
                    public void onError(Call call, Exception e) {

                    }

                    @Override
                    public void onResponse(Element response) {
                        if (response != null) {
                            String message = response.elementText("message");
                            String resultCode = response.elementText("resultCode");
                            if ("10000".equals(resultCode)) {
                                Element cardInfo = response.element("cardInfo");
                                Element peopleInfo = cardInfo.element("peopleInfo");
                                if (peopleInfo != null) {
                                    String sfzhm = peopleInfo.elementText("sfzhm");
                                    String xm = peopleInfo.elementText("xm");
                                    String xb = peopleInfo.elementText("xb");
                                    if ("1".equals(xb)) {
                                        xb = "男";
                                    } else if ("2".equals("xb")) {
                                        xb = "女";
                                    }
                                    tvSfzhm.setText(sfzhm);
                                    tvXm.setText(xm);
                                    tvXb.setText(xb);
                                    Log.i("xx==", sfzhm + "," + xm + "," + xb);
//                                  String nl=peopleInfo.elementText("")

                                }
                            } else {
                                Toast.makeText(getApplicationContext(), message + "", Toast.LENGTH_SHORT).show();
                            }
                        }

                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
